<template>
	<view class="address">
		<view class="head">
			<view class="title">
				地址簿
			</view>
			<navigator class="btn" url="/pages_shop/address/edit">
				添加地址
			</navigator>
		</view>
		<view class="list" v-for="(item,index) in dataList" @click="clickSET(item)">
			<view class="top">
				<view class="user">
					{{item.hostname}}-{{item.hostphone}}
				</view>
				<view class="select">
					<!-- stop阻止冒泡 -->
					<u-button @click.native.stop="clickbtn(index)" class="btn" :class="selectId==index ? 'active' :'' "
						:type=" selectId==index ? 'primary' :''" size="mini" text="设为默认"></u-button>
				</view>
			</view>
			<view class="more">
				{{item.content}}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		GetStroage
	} from "@/utils/tool.js"
	import {
		mapGetters,
		mapMutations
	} from "vuex"
	export default {
		data() {
			return {
				selectId: null,
				userInfo: "",
				dataList: [],
			}
		},
		onShow() {
			this.userInfo = GetStroage("userInfo");
			setTimeout(() => {
				this.getData();
			}, 100)
		},
		computed: {
			...mapGetters(["addressObj"]),
		},
		methods: {
			...mapMutations(["setAddress"]),
			getData() {
				uni.request({
					url: this.$e.Url + "/address/getUserAll",
					data: {
						uid: this.userInfo.id
					},
					method: "GET",
					success: (res) => {
						console.log("请求API接口【/address/getUserAll】", res);
						this.dataList = res.data;
						for (let index = 0; index < res.data.length; index++) {
							if (res.data[index].isDefault == true) { //刚开始进入页面,如果查到有默认的,那就把刚开始的高亮给默认值
								this.selectId = index;
							}
						}
					}
				})
			},
			clickSET(obj) {
				// console.log("点击的obj是:", obj);
				this.setAddress(obj);
				// console.log("vuex:", this.addressObj);
				uni.navigateBack();
			},
			clickbtn(index) {
				console.log("设为默认");
				console.log("----", this.dataList, this.selectId);
				if (!this.selectId) {
					this.selectId = index;//新建第一个地址的时候,没有默认地址,那就让默认地址=选择的地址,先让默认地址为0,然后让选择地址为1
				}
				uni.request({
					url: this.$e.Url + "/address/modifyUserDefault",
					data: {
						id: this.dataList[index].id,//选择的address
						selectId: this.dataList[this.selectId].id//默认address
					},
					method: "PUT",
					header: {
						"Content-Type": "application/json"
					},
					success: (res) => {
						console.log("请求API接口【/address/modifyUserDefault】", res);
						this.selectId = index; //修改成功之后再让selectId赋值
					}
				});

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-image: url("/static/userbackground.jpg");
		background-size: cover;
		background-repeat: repeat;
		background-position: center center;
	}

	.address {
		padding: 30rpx;

		.head {
			@include flex-box();
			padding: 0 10rpx;

			.title {
				font-size: 35rpx;
				font-weight: bold;
				font-family: "微软雅黑";
			}

			.btn {
				border: 1px solid #bbb;
				border-radius: 10rpx;
				padding: 6rpx 15rpx;
			}
		}

		.list {
			background: #fff;
			padding: 30rpx 20rpx;
			margin: 20rpx 0;
			border-radius: 10rpx;
			border: 1rpx solid #eee;

			.top {
				@include flex-box();

				.user {}

				.select {
					.btn {
						color: #bbb;
						border: 1px solid #bbb;

						&.active {
							color: #fff;
						}
					}
				}
			}


			.more {
				font-size: 28rpx;
				color: $text-font-color-3;
			}
		}
	}
</style>
